const Lettering = '刻字'
let workbenchBtnService = {
  computed: {
    // 登录用户id
    userId() {
      return this.$store.state.userInfo.staffId || ''
    },
    // 周转进度
    batchProcessStatus() {
      let list = this.$store.state.dict.batchProcessStatus.labels || []
      return list.filter((item) => item.value === 'ToBeStarted' || item.value === 'Producing' || item.value === 'Finished')
    },
    // 周转进度生产中、已完成状态的ID
    batchProcessStatusIds() {
      return this.batchProcessStatus.reduce((pre, cur) => [...pre, cur.id], []).join(',')
    },
    // 自检id
    inspectionId() {
      let list = this.$store.state.dict.inspectionType.labels || []
      let item = list.find(item => item.value === 'Self') || {id: ''}
      return item.id
    },
    // 验证是否已选
    _selected() {
      if (!this.selectData.length) return {msg: '请选择一条工序！'}
    },
    // 获取序列号按钮状态
    serialNumberBtnStatus() {
      if (this._selected) return this._selected
      let status = false
      let msg = '请选择一个"刻字"工序'
      if (this.selectData.some(item => item.workstageName.indexOf(Lettering) === -1) || this.selectData.length > 1) {
        status = true
      }
      return status ? {disabled: status, msg: msg} : {msg: false}
    },
    // 参数记录
    paramBtnStatus() {
      if (this._selected) return this._selected
      let status = false
      const msg = '请选择工序进度为非"待开工、未到达" , 工序名称相同 , 还未记录模板且模板名称相同的工序'
      if (this.selectData.some((item) => item.workstageName !== this.selectData[0].workstageName)) {
        status = true
      }
      if (this.selectData.some((item) => item.workstageProcessStatusValue === 'ToBeStarted' || item.workstageProcessStatusValue === 'NotArrive')) {
        status = true
      }
      if (this.selectData.some((item) => item.paramTemplateName !== this.selectData[0].paramTemplateName)) {
        status = true
      }
      if (this.selectData.some((item) => !item.paramTemplateId)) {
        status = true
      }
      if (this.selectData.length > 1 && this.selectData.some((item) => item.parameterRecordId)) {
        status = true
      }
      return status ? {disabled: status, msg: msg} : {msg: false}
    },
    // 工序上料
    materialBtnStatus() {
      if (this._selected) return this._selected
      let status = false
      const msg = '请选择工序名称相同 , 物料编码相同 , 周转状态为"正常" , 工序进度为"已开工" , 单条上料状态为"上料中"或"已上料"且均为同一工单能够上料的工序'
      if (this.selectData.some((item) => item.materialStatusValue === 'Processing') && this.selectData.length > 1) {
        status = true
      }
      if (this.selectData.some((item) => item.materialStatusValue === 'Finished') && this.selectData.length > 1) {
        status = true
      }
      if (this.selectData.some((item) => item.workorderNumber !== this.selectData[0].workorderNumber)) {
        status = true
      }
      if (this.selectData.some((item) => item.workstageName !== this.selectData[0].workstageName)) {
        status = true
      }
      if (this.selectData.some((item) => item.materialNumber !== this.selectData[0].materialNumber)) {
        status = true
      }
      if (this.selectData.some((item) => item.turnoverStatusValue !== 'Normal')) {
        status = true
      }
      if (this.selectData.some((item) => item.workstageProcessStatusValue !== 'Started')) {
        status = true
      }
      if (this.selectData.some((item) => item.materialStatusValue === 'Unneeded')) {
        status = true
      }
      return status ? {disabled: status, msg: msg} : {msg: false}
    },
    // 完工登记
    completeBtnStatus() {
      if (this._selected) return this._selected
      let status = false
      const turnoverIds = this.selectData.map((item) => item.turnoverId)
      const clearArr = Array.from(new Set(turnoverIds))
      const msg = '请选择不同周转批次且周转状态为"正常" , 工序进度为"已开工" , 上料状态为"已上料"或"无需上料"的工序'
      if (this.selectData.some((item) => item.turnoverStatusValue !== 'Normal')) {
        status = true
      }
      if (this.selectData.some((item) => item.workstageProcessStatusValue !== 'Started')) {
        status = true
      }
      if (this.selectData.some((item) => item.materialStatusValue !== 'Finished' && item.materialStatusValue !== 'Unneeded')) {
        status = true
      }
      if (turnoverIds.length !== clearArr.length) {
        status = true
      }
      return status ? {disabled: status, msg: msg} : {msg: false}
    },
    // 开工登记
    startBtnStatus() {
      if (this._selected) return this._selected
      let status = false
      const msg = '请选择周转状态为"正常" , 工序进度为"待开工"的工序'
      if (this.selectData.some((item) => item.turnoverStatusValue !== 'Normal')) {
        status = true
      }
      if (this.selectData.some((item) => item.workstageProcessStatusValue !== 'ToBeStarted')) {
        status = true
      }
      return status ? {disabled: status, msg: msg} : {msg: false}
    },
  },
  methods: {
    // 洁净间
    recordCleanRoom() {
      this.$refs.RecordCleanRoom.openDialog()
    },
    /**
     * 打开设备状态
     */
    equipmentState() {
      this.$refs.EquipmentState.openDialog()
    },
    /**
     * 获取序列号
     */
    getSerialNumber() {
      this.$refs.GetSerialNumber.openDialog(this.selectData)
    },
    // 参数记录
    paramRecord() {
      this.$refs.WorkStageParamRecord.openDialog(this.selectData)
    },
    // 工序上料
    WorkStageMaterial() {
      this.$refs.WorkStageMaterial.openDialog(this.selectData)
    },
    /**
     * 完工登记
     */
    completeRegister() {
      this.$refs.CompleteRegister.openDialog(this.selectData, 'workShopBench')
    },
    // 开工登记
    workStart() {
      if (this.selectData.some((item) => item.inputQuantity === 0)) {
        return this.tipMessage('error', '投料数量为0的工序不能开工，请通知班组长终止任务')
      }
      this.$refs.workStageStart.openDialog(this.selectData)
    },
  }
}
export default workbenchBtnService
